package com.mybatisplus.socket_test1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Auther: GongXl
 * @Date: 2021/3/2 09:54
 * @Description:服务端：client => 1:1
 */
public class MultiThreadServer implements Runnable{
    private Socket socket;
    public MultiThreadServer(Socket socket) {
        this.socket = socket;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
            try {
                DataInputStream is = new DataInputStream(socket.getInputStream());
                DataOutputStream os = new DataOutputStream(socket.getOutputStream());
                while (true) {
                    String line = is.readUTF();
                    System.out.println("Client "+socket.hashCode()+"说：" + line);
                    TCPServer03.sendToAll(socket,line);
                    if (line.equals("exit")) {
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}